package org.example.mq;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/6/28
 * Email haijunzhou@hnit.edu.cn
 */
@RestController
@RequestMapping("mq")
public class MQAction {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("work")
    public String work(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("workQueue", msg + " " + i);
        }
        return "工作模式消息发送成功!";
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void working1(String msg){
        System.out.println("working1 "+msg);
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void working2(String msg){
        System.out.println("working2 "+msg);
    }

    ///////////////////////////////////////////////////////
    @RequestMapping("fanout")
    public String fanout(String msg){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("fanoutEx", "", msg + " " + i);
        }
        return "广播模式消息发送成功!";
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(name="fanoutEx", type = "fanout")
    ))
    public void fanouting1(String msg){
        System.out.println("fanouting1 "+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "fanoutQueue"),
            exchange = @Exchange(name="fanoutEx", type = "fanout")
    ))
    public void fanouting2(String msg){
        System.out.println("fanouting2 "+msg);
    }

    ///////////////////////////////////////////////////////
    @RequestMapping("topic")
    public String topic(String msg, String key){
       rabbitTemplate.convertAndSend("topicEx", key, msg);
        return "主题模式消息发送成功!";
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(name="topicEx", type = "topic"),
            key = {"user.insert", "#.product.*"}
    ))
    public void topicing1(String msg){
        System.out.println("topicing1 "+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "topicQueue"),
            exchange = @Exchange(name="topicEx", type = "topic"),
            key = {"user.save", "order.*", "cart.#"}
    ))
    public void topicing2(String msg){
        System.out.println("topicing2 "+msg);
    }

}
